<!--
 * @author hemengxia
 * @desc 红包活动列表
-->
<template>
  <section class="red-packet-main">
    <el-collapse-transition>
      <section class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="listQuery.title"
          placeholder="输入活动名称搜索"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <section class="search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
        </section>
      </section>
    </el-collapse-transition>

    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>活动红包配置</span>
      </section>
      <section class="main-buttons-btns">
        <el-button style="margin-left: 10px" @click="handleCreate">新建</el-button>
      </section>
    </section>

    <!--		<section class="main-tab">
          <el-tabs v-model="active" @tab-click="handleTabClick">
            <el-tab-pane v-for="item in types" :key="item.id" :label="item.name" :name="item.id" />
          </el-tabs>
        </section>-->

    <section class="main-table">
      <v-table
        :list="list"
        tag="redPacketMain"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
        @handleSelectionChange="handleSelectionChange"
      >
        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          min-width="100"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleStatus(scope.row)">
              {{ scope.row.publishStatus == "RPAPUBSTATUS1" ? "取消发布" : "发布" }}
            </el-button>
            <el-button type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
            <el-button
              v-if="scope.row.limitUser===1"
              type="text"
              size="mini"
              @click="handleUser(scope.row)"
            >参与人员
            </el-button>
            <el-button
              v-if="scope.row.limitDept===1"
              type="text"
              size="mini"
              @click="handleDept(scope.row)"
            >参与部门
            </el-button>
            <el-button type="text" size="mini" @click="handleRecharge(scope.row)">充值记录</el-button>
            <el-button
              v-if="scope.row.publishStatus == 'RPAPUBSTATUS1'"
              type="text"
              size="mini"
              @click="handleRecord(scope.row)"
            >领取信息
            </el-button>

          </template>
        </el-table-column>
      </v-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />

    </section>
    <recharge-record v-if="showRecord" :id="vid" :acc-id="accId" @cancel="handleCancel" />
    <department v-if="showDepartment" :id="vid" @cancel="handleCancel" />
  </section>
</template>
<script>

import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import {
  getDictByCode,
  queryPage,
  updateState,
  del
} from './../api/index'
import permission from '@/directive/permission/index.js'
import elDragDialog from '@/directive/el-drag-dialog'
import RechargeRecord from '@/views/activity/redPacket/components/rechargeRecord'
import Department from '@/views/activity/redPacket/components/department'

export default {
  name: 'RedPacketMain',
  components: {
    Pagination,
    VTable,
    RechargeRecord,
    Department
  },
  directives: {
    permission,
    elDragDialog
  },
  data() {
    return {
      i: '',
      listQuery: {
        page: 1,
        limit: 10,
        title: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      multipleSelection: [],
      showRecord: false,
      vid: '',
      accId: '',
      showDepartment: false

    }
  },

  created() {
    if (this.$route.query.t) {
      this.active = this.$route.query.t
    }
    this.getList()
  },
  methods: {

    filtercolumn(scope, item) {
      if (item === 'receiveTime') {
        return (scope.row['receiveStarttime'] && scope.row['receiveStarttime'].substring(0, 10)) + '至' + (scope
          .row['receiveEndtime'] && scope.row['receiveEndtime'].substring(0, 10))
      } else if (item === 'expirationTime') {
        let str = ''
        if (scope.row['expirationStartdate'] && scope.row['expirationStartdate'] !== undefined) {
          str += scope.row['expirationStartdate'].substring(0, 10)
        }
        if (scope.row['expirationDate'] && scope.row['expirationDate'] !== undefined) {
          str += '至' + scope.row['expirationDate'].substring(0, 10)
        }
        return str
      } else if (item === 'classifType2') {
        return scope.row[item] || '--'
      } else {
        return scope.row[item]
      }
    },

    /**
     * 选项卡切换
     */
    handleTabClick(tab, event) {
      this.listQuery.page = 1
      this.getList()
    },
    handleSelectionChange(arr) {
      this.multipleSelection = arr
    },

    handleReset() {
      this.listQuery.page = 1
      this.listQuery.title = ''
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleCreate() {
      // this.$router.push({path: '/activity/activityConfig/redPacket/manage', query: {type: 'c'}})
      this.$router.push({ path: this.$filterPath('manage'), query: { type: 'c' }})
    },

    handleUpdate(row) {
      this.$router.push({ path: this.$filterPath('manage'), query: { i: row.id, type: 'u' }})
    },
    /**
     * 领取信息
     * */
    handleRecord(row) {
      // this.$router.push({path: '/activity/activityConfig/redPacket/record', query: {i: row.id,}})
      this.$router.push({ path: this.$filterPath('record'), query: { i: row.id }})
    },
    /**
     * 设置充值记录
     * @param row
     */
    handleRecharge(row) {
      this.vid = row.id
      this.accId = row.accountId
      this.showRecord = true
    },

    handleCancel() {
      this.showRecord = false
      this.showDepartment = false
      this.getList()
    },
    handleStatus(row) {
      let msg = ''
      if (row.publishStatus === 'RPAPUBSTATUS1') {
        msg = '取消发布'
      } else {
        msg = '发布'
      }
      this.$confirm('确定' + msg + '该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        return new Promise((resolve, reject) => {
          updateState({
            id: row.id
          }).then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: msg + '成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch(() => {
      })
    },
    /**
     * 人员设置
     * */
    handleUser(row) {
      /* this.$router.push({
         path: '/activity/activityConfig/redPacket/user',
         query: {
           id: row.id,
         }
       })  */
      this.$router.push({
        path: this.$filterPath('user'),
        query: {
          id: row.id
        }
      })
    },
    /**
     * 部门设置
     * @param row
     */
    handleDept(row) {
      this.vid = row.id
      this.showDepartment = true
    },

    handleDelete(ids) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        return new Promise((resolve, reject) => {
          del({ id: ids }).then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch(() => {
      })
    },

    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode(code).then(res => {
          this.dict[code] = (res.data.data && res.data.data.length > 0) ? res.data.data[0]
            .list : []
        }).catch(error => {
          reject(error)
        })
      })
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        title: this.listQuery.title
      }
      return new Promise((resolve, reject) => {
        queryPage(query).then(response => {
          this.list = response.data.data.rows
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>
<style lang="scss">
.red-packet-main {
  padding: 10px;

  .main-search {
    background-color: #fff;
    padding: 0 10px;

    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }

    .search-input {
      width: 60%;
      position: relative;

      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }

      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;

        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }

    .search-btn {
      display: inline-block;
      float: right;

      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }

      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }
  }

  .main-buttons {
    display: flex;
    line-height: 36px;
    margin-top: 10px;

    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }

  .main-tab {
    width: 100%;
    margin-top: 10px;
    padding: 0 10px;
    background-color: #fff;
    display: flex;
    border-radius: 4px;

    .el-tabs__nav-wrap::after {
      display: none;
    }

    .el-tabs__header {
      margin: 0;
    }
  }

  .inner-explain {
    margin-top: 10px;
    line-height: 1.1;
    color: #999;
    font-size: 14px;
    display: flex;
    align-items: flex-start;

    > i {
      margin-right: 5px;
    }

    p {
      margin: 0;
      padding: 0;
      margin-bottom: 5px;
    }
  }
}
</style>
